<?php
/**
 * 角色组控制器
 * User: xuyiping(xuyiping@liangzihuzhu.com.cn)
 * Date: 2017/11/29
 * Time: 14:29
 */

namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Controller;
use App\Model\User\RoleGroupModel;
use App\Model\User\RoleModel;
use Illuminate\Http\Request;


class RoleGroupController extends Controller
{

    public function index(){
        return view('Admin/system/role/group');
    }

    public function search(Request $request){
        $where = [];

        //查询条件
        if ($request->input('roleGroupName')) {
            $where['roleGroupName'] = $request->input('roleGroupName');
        }

        $groups = RoleGroupModel::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $res_data = array(
            'total' => $groups['total'],
            'rows' => array()
        );
        foreach ( $groups['data'] as &$row ) {

            $operation = '<a href="javascript: void(0);" onclick="RoleGroup.del(' . $row['roleGroupID'] . ');">删除</a>&nbsp&nbsp';
            $operation .= '<a href="javascript: void(0);" onclick="RoleGroup.edit(' . $row['roleGroupID'] . ');">修改</a>&nbsp&nbsp';

            $res_data['rows'][] = array(
                'operation' => $operation,
                'roleGroupName' => $row['roleGroupName'],
                'memo' => $row['memo'],
            );
        }

        return response()->json($res_data);
    }

    public function store(Request $request){

        //新增或修改角色组信息
        if ($request->input('roleGroupID')) {
            $role_group = RoleGroupModel::find($request->input('roleGroupID'));
            if(!$role_group){
                return response()->json([
                    'code' => 1000001,
                    'message' => '角色组编辑提供的信息无效，请尝试刷新后重试'
                ]);
            }
            if($role_group->roleGroupName != $request->input('roleGroupName')){
                //更改角色表中的角色组名称
                RoleModel::where('roleGroupID',$request->input('roleGroupID'))->update(['roleGroupName'=>$request->input('roleGroupName')]);
            }
        } else {
            $role_group = new RoleGroupModel();

            $role_group->uuid = makeUuid();
            $role_group->timeStamp = date('Y-m-d H:i:s');
            $role_group->createTime = date('Y-m-d H:i:s');
            $role_group->creator = session('ADMIN_USER_ID');

        }
        $role_group->roleGroupName = $request->input('roleGroupName');
        $role_group->memo = $request->input('memo');
        $role_group->save();

        return response()->json([
            'code' => 200,
            'message' => 'ok'
        ]);

    }

    public function get($id)
    {

        $role_group = RoleGroupModel::select('roleGroupID', 'roleGroupName', 'memo')->find($id);

        if ($role_group) {
            return response()->json([
                'code' => 200,
                'message' => 'ok',
                'data' => $role_group
            ]);
        } else {
            return response()->json([
                'code' => 404,
                'message' => '角色组信息没有找到'
            ]);
        }
    }

    /**
     * 删除角色组
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id)
    {
        //角色组信息
        $role_group = RoleGroupModel::find($id);
        if(!$role_group){
            return response()->json([
                'code' => 1000001,
                'message' => '数据无效，请尝试刷新数据后重试'
            ]);
        }
        //判断下面是否有角色
        if( RoleModel::where('roleGroupID',$id)->first()){
            return response()->json([
                'code' => 1000002,
                'message' => '该角色组下有角色，请先处理角色'
            ]);
        };
        //判断是否为系统管理员
        if( $role_group['roleGroupID']== 1 ){
            return response()->json([
                'code' => 1000003,
                'message' => '系统管理员组不可删除！'
            ]);
        }
        //删除角色组
        $role_group->delete();

        return response()->json([
            'code' => 200,
            'message' => 'ok'
        ]);

    }
}